import { VmoRequest } from "vmo-request";

export class DashVector{
  private endPoint:string
  private apiKey:string
  private vmoRequest:VmoRequest
  public constructor(endPoint:string,apiKey:string){
    this.endPoint=endPoint;
    this.apiKey=apiKey
    this.vmoRequest = new VmoRequest({
      baseURL:`https://${this.endPoint}`,
      method:'post',
      headers:{
        "dashvector-auth-token":this.apiKey,
        "Content-Type":"application/json"
      }
    })
  }
  public async create(data:{name:string,dimension:number}){
    try{
      return await this.vmoRequest.request({
        url:"/v1/collections",
        data,
      })
    }catch(err){
      throw err
    }
  }
  public async insert(collection:string,docs:{id?:string,vector:number[],fields?:Record<string,any>}[]){
    try{
      return await this.vmoRequest.request({
        url:`/v1/collections/${collection}/docs`,
        data:{
          docs
        },
      })
    }catch(err){
      throw err
    }
  }
  public async query(collection:string, query:{vector:number[], filter?:string, sparse_vector?:Record<string,number>, topk:number}){
    try{
      return await this.vmoRequest.request({
        url:`/v1/collections/${collection}/query`,
        data:query,
      })
    }catch(err){
      throw err
    }
  }
  public async remove(collection:string, ids:string[]=[]){
    try{
      return await this.vmoRequest.request({
        method:'delete',
        url:`/v1/collections/${collection}/docs`,
        data:{
          ids
        },
      })
    }catch(err){
      throw err
    }
  }
  public async stats(collection:string){
    try{
      return await this.vmoRequest.request({
        method:'get',
        url:`/v1/collections/${collection}/stats`,        
      })
    }catch(err){
      throw err
    }
  }
  public async delete(collection:string){
    try{
      return await this.vmoRequest.request({
        method:'get',
        url:`/v1/collections/${collection}`,
      })
    }catch(err){
      throw err
    }
  }
}